namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("img")]
    public class Image : Element, IAttributeAlternativeText, IXmlSerializable
    {
        public Image()
        {
        }

        public Image(Uri source, int height, int width, string alternative)
        {
            this.Source = source;
            this.Height = height;
            this.Width = width;
            this.AlternativeText = alternative;
        }

        public string AlternativeText
        {
            get;
            set;
        }

        public int? Height
        {
            get;
            set;
        }

        public Uri LongDescription
        {
            get;
            set;
        }

        public Uri Source
        {
            get;
            set;
        }

        public int? Width
        {
            get;
            set;
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if (null == type && "img".Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<Image>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            if (XmlNodeType.Element == reader.NodeType)
            {
                string value = reader.GetAttribute("alt");
                if (!string.IsNullOrEmpty(value))
                {
                    this.AlternativeText = value;
                }

                value = reader.GetAttribute("height");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Height = XmlConvert.ToInt32(value);
                }

                value = reader.GetAttribute("longdesc");
                if (!string.IsNullOrEmpty(value))
                {
                    this.LongDescription = new Uri(value);
                }

                value = reader.GetAttribute("src");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Source = new Uri(value, UriKind.RelativeOrAbsolute);
                }

                value = reader.GetAttribute("width");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Width = XmlConvert.ToInt32(value);
                }
            }

            base.ReadXml(reader);
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            if (!string.IsNullOrEmpty(this.AlternativeText))
            {
                writer.WriteAttributeString("alt", this.AlternativeText);
            }

            if (this.Height.HasValue)
            {
                writer.WriteAttributeString("height", XmlConvert.ToString(this.Height.Value));
            }

            if (null != this.LongDescription)
            {
                writer.WriteAttributeString("longdesc", this.LongDescription.IsAbsoluteUri ? this.LongDescription.AbsoluteUri : this.LongDescription.OriginalString);
            }

            if (null != this.Source)
            {
                writer.WriteAttributeString("src", this.Source.IsAbsoluteUri ? this.Source.AbsoluteUri : this.Source.OriginalString);
            }

            if (this.Width.HasValue)
            {
                writer.WriteAttributeString("width", XmlConvert.ToString(this.Width.Value));
            }
        }
    }
}